/*
 * Order.cpp
 *
 *  Created on: Jun 22, 2013
 *      Author: amatzlia
 */

#include "Order.h"

Order::Order(int id) :
		id(id), paid(false) {
	if (id <= 0) {
		throw mtm::INVALID_ORDER_NUMBER();
	}
}

void Order::addAlbum(const Album& album) {
	verifyNotPaid();
	albums.insert(album);
}

void Order::removeAlbum(const Album& album) {
	verifyNotPaid();

	bool albumFound = false;

	Album::BarcodeEquals predicate(album);
	List<Album>::Iterator albumIterator = albums.find(predicate);
	albumFound = albumIterator != albums.end();

	while (albumIterator != albums.end()) {
		albums.remove(albumIterator);
		albumIterator = albums.find(predicate);
	}

	if (!albumFound) {
		throw mtm::ORDER_DOESNT_HAVE_ALBUM();
	}
}

int Order::getId() const {
	return id;
}

const List<Album> Order::getAlbums() const {
	return albums;
}

void Order::pay() {
	verifyNotPaid();
	paid = true;
}

string Order::getOrderReport(int customerId) const {
	string output = mtm::getOrderHeaderString(id, customerId, albums.getSize(),
			paid);

	for (const Album& album : albums) {
		output += album.getMinimalString();
	}

	return output;
}

bool Order::SortById::operator()(Order orderA, Order orderB) const {
	return orderA.id < orderB.id;
}

void Order::verifyNotPaid() {
	if(paid) {
		throw mtm::ORDER_WAS_ALREADY_PAID();
	}
}
